package com.kb.service.impl.gc;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.kb.entity.User;
import com.kb.mapper.mysql.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@DS("master")
public class CallLimitService {

    private static final int MIN_CALLS_PER_DAY = 0;

    @Autowired
    private UserDao userMapper;

    public boolean canCall(String userName) {
        User user = userMapper.selectByUserName(userName);
        if (user == null) {
            return false;
        }
        int currentCount = user.getDailyCallCount();
        if (currentCount <= MIN_CALLS_PER_DAY) {
            return false;
        }

        user.setDailyCallCount(currentCount - 1);
        userMapper.updateById(user);
        return true;
    }

    public void resetCallCount() {
        userMapper.resetDailyCallCount();
    }
}
